package com.exercise.v1;

import java.util.ArrayList;
import java.util.List;

public class Exercise {
    private List<Expression> expressions;
    private String title;
    public Exercise() {
        this.expressions = new ArrayList<>();
        this.title = "数学练习";
    }
    public Exercise(String title) {
        this.expressions = new ArrayList<>();
        this.title = title;
    }
    public void addExpression(Expression expression) {
        expressions.add(expression);
    }
    public void addExpression(int operand1, int operand2, String operator) {
        expressions.add(new Expression(operand1, operand2, operator));
    }
    public List<Expression> getExpressions() {
        return expressions;
    }
    public int getSize() {
        return expressions.size();
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Expression getExpression(int index) {
        if (index >= 0 && index < expressions.size()) {
            return expressions.get(index);
        }
        return null;
    }
    public void clear() {
        expressions.clear();
    }
    public String formatDisplay() {
        StringBuilder sb = new StringBuilder();
        sb.append("========== ").append(title).append(" ==========\n");
        for (int i = 0; i < expressions.size(); i++) {
            sb.append(String.format("%2d. %s\n", i + 1, expressions.get(i).toString()));
        }
        sb.append("==========================================\n");
        return sb.toString();
    }
    public String formatDisplayQuestions() {
        StringBuilder sb = new StringBuilder();
        sb.append("========== ").append(title).append(" ==========\n");
        for (int i = 0; i < expressions.size(); i++) {
            sb.append(String.format("%2d. %s\n", i + 1, expressions.get(i).toQuestionString()));
        }
        sb.append("==========================================\n");
        return sb.toString();
    }
}

